//
//  DLFormBgView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/30.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLFormBgView.h"
#import "DLFormScrollView.h"
#import "DLMachineModel.h"

@interface DLFormBgView()

@property (nonatomic, strong) NSMutableArray *leftTableDataSource;

@property (nonatomic, strong) UILabel *noDataLabel;

@end


@implementation DLFormBgView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kColorRGB(46, 56, 70);
        self.backgroundColor = [UIColor whiteColor];
        self.leftTableDataSource = [NSMutableArray array];
        
        
        
        [self layoutSubControls];
    }
    return self;
}
- (void) layoutSubControls {
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(12, 20, self.width - 24, kMachineFormTitleLabelHeight);
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.text = @"热门基金";
    titleLabel.font = kSystemFont(18);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
//    titleLabel.backgroundColor = [UIColor redColor];
    
    UILabel *formFirstLabel = [UILabel new];
    formFirstLabel.frame = CGRectMake(titleLabel.left, titleLabel.bottom, kMachineFormContentTitleLabelWidth, titleLabel.height);
    formFirstLabel.textColor = kDLNormalGrayColor;
    formFirstLabel.text = @"基金/股票名称";
    formFirstLabel.font = kSystemFont(12);
    formFirstLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:formFirstLabel];
    self.formFirstLabel = formFirstLabel;
    formFirstLabel.backgroundColor = kColorRGB(46, 56, 70);
    formFirstLabel.backgroundColor = [UIColor whiteColor];
    
//    UIScrollView *formTitleBgSV = [UIScrollView new];
//    formTitleBgSV.frame = CGRectMake(formFirstLabel.right, formFirstLabel.top, titleLabel.width - formFirstLabel.width, formFirstLabel.height);
//    formTitleBgSV.showsVerticalScrollIndicator = NO;
//    formTitleBgSV.showsHorizontalScrollIndicator = NO;
//    formTitleBgSV.backgroundColor = [UIColor yellowColor];
//    [self addSubview:formTitleBgSV];
//    self.formTitleBgSV = formTitleBgSV;
    
    UITableView *leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(titleLabel.left, formFirstLabel.bottom, formFirstLabel.width, kMachineFormTableViewHeight) style:UITableViewStylePlain];
    leftTableView.delegate = self;
    leftTableView.dataSource = self;
//    leftTableView.showsVerticalScrollIndicator = NO;
//    leftTableView.showsHorizontalScrollIndicator = NO;
    leftTableView.scrollEnabled = NO;
    leftTableView.rowHeight = kMachineFormTableViewRowHeight;
    leftTableView.backgroundColor = [UIColor redColor];
    leftTableView.backgroundColor = kColorRGB(46, 56, 70);
    leftTableView.backgroundColor = [UIColor whiteColor];
    [self addSubview:leftTableView];
    self.leftTableView = leftTableView;
    [leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"videoUrl"];
    leftTableView.separatorColor = kColorRGB(232, 232, 232);
    
    DLFormScrollView *formScrollView = [[DLFormScrollView alloc] initWithFrame:CGRectMake(leftTableView.right, formFirstLabel.top, titleLabel.width - leftTableView.width, leftTableView.height + formFirstLabel.height)];
    formScrollView.backgroundColor = [UIColor redColor];
    formScrollView.backgroundColor = kColorRGB(46, 56, 70);
    formScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:formScrollView];
    self.formScrollView = formScrollView;
    
    
    formScrollView.contentTCTapLabelBlock = ^(UILabel * _Nonnull tapLabel, CGRect frameToSsuperView) {
//        NSLog(@"text:%@", tapLabel.text);
//        NSLog(@"frame:%@", NSStringFromCGRect(frameToSsuperView));
        
        
        UILabel *wholeContenLabel = [UILabel new];
        wholeContenLabel.frame = CGRectMake(10, frameToSsuperView.origin.y, self.width - 20, 40);
        wholeContenLabel.backgroundColor = kColorRGB(240, 240, 240);
        wholeContenLabel.text = tapLabel.text;
        wholeContenLabel.textAlignment = NSTextAlignmentCenter;
        wholeContenLabel.font = kSystemFont(12);
        wholeContenLabel.textColor = kDLNormalBlackColor;
        [self addSubview:wholeContenLabel];
        
        
        CGFloat labelTextWidth = 0;
        labelTextWidth = [tapLabel.text getValue4WidWithHeight:kMachineFormTitleLabelHeight andFont:kSystemFont(12)] + 20;
        
        CGFloat labelTextHeight = 0;
        labelTextHeight = [tapLabel.text getValue4HeightWithWid:self.width - titleLabel.left *2 andFont:kSystemFont(12)];
        labelTextHeight = kMachineFormTitleLabelHeight;
        
        if (labelTextWidth > self.width - titleLabel.left *2) {
            labelTextWidth = self.width - titleLabel.left *2;
        }
        
//        if (labelTextHeight < kMachineFormTitleLabelHeight) {
//            labelTextHeight = kMachineFormTitleLabelHeight;
//        }
//        [wholeContenLabel resetWidth:labelTextWidth];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.5 animations:^{
                wholeContenLabel.alpha = 0;
            } completion:^(BOOL finished) {
                [wholeContenLabel removeFromSuperview];                
            }];
        });
    };
    
    
    
    UIButton *showAllButton = [UIButton buttonWithType:0];
    showAllButton.frame = CGRectMake(leftTableView.left, self.height - kMachineFormBottomBtnHeight, titleLabel.width, kMachineFormBottomBtnHeight);
    [showAllButton setTitle:@"展开全部" forState:UIControlStateNormal];
    [showAllButton setTitle:@"收起" forState:UIControlStateSelected];
    showAllButton.titleLabel.font = kSystemFont(13);
    showAllButton.backgroundColor = kDLBtnNormalBgColor;
    [showAllButton addTarget:self action:@selector(showAllButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:showAllButton];
    self.showAllButton = showAllButton;
    
    UILabel *noDataLabel = [UILabel new];
    noDataLabel.frame = CGRectMake(0, leftTableView.top, self.width, kMachineFormTableViewHeight + kMachineFormTitleLabelHeight);
    noDataLabel.text = @"暂无数据";
    noDataLabel.textAlignment =NSTextAlignmentCenter;
    noDataLabel.font = kSystemFont(30);
    noDataLabel.textColor = kDLNormalBlackColor;
    noDataLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:noDataLabel];
    self.noDataLabel = noDataLabel;
    noDataLabel.hidden = YES;
}
- (void) showAllButtonClick:(UIButton *) button {
    button.selected = !button.selected;
    self.formBgViewModel.isFormShowAllData = !self.formBgViewModel.isFormShowAllData;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"plainTableShouldReloadData" object:nil];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.leftTableDataSource.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoUrl"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor blackColor];
    cell.backgroundColor = kColorRGB(46, 56, 70);
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.leftTableDataSource[indexPath.row]];
    cell.textLabel.font = kSystemFont(12);
    cell.textLabel.textColor = kColorRGB(85, 85, 85);;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}
- (void) setFormBgViewModel:(DLMachineModel *)formBgViewModel {
    _formBgViewModel = formBgViewModel;
    
    //新加的 尝试
    if (formBgViewModel.isFormShowAllData == YES) {
        self.showAllButton.selected = YES;
    } else {
        self.showAllButton.selected = NO;
    }
    
    NSDictionary *dic = self.formBgViewModel.resultDic[@"data"];
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@", dic[@"pTitle"]];
    
    NSArray *titArr = dic[@"ColLabel"];
    self.formFirstLabel.text = [NSString stringWithFormat:@"%@", titArr[0]];
    
    NSArray *firstColArr = dic[@"kData"];
    if (firstColArr.count > 0) {
        
        self.noDataLabel.hidden = YES;
        
        [self.leftTableDataSource removeAllObjects];
        
        //这里判断如果图表只有8条以下的数据，就全部展示 不需要展开收起功能 同时隐藏展开收起按钮
        if (firstColArr.count < 9) {
            
            self.showAllButton.hidden = YES;
            
            for (NSInteger i = 0; i < firstColArr.count; i++) {
                
                NSArray *arr = firstColArr[i];
                if ([arr isKindOfClass:[NSArray class]]) {
                    [self.leftTableDataSource addObject:arr[0]];
                }
            }
            [self resetHeight:firstColArr.count *kMachineFormTableViewRowHeight + kMachineFormTitleLabelHeight *2 + 40];
            [self.leftTableView resetHeight:firstColArr.count *kMachineFormTableViewRowHeight];
            [self.formScrollView resetHeight:self.leftTableView.height + kMachineFormTitleLabelHeight];
        } else {
            
            self.showAllButton.hidden = NO;
            
            for (NSInteger i = 0; i < firstColArr.count; i++) {
                
                NSArray *arr = firstColArr[i];
                if ([arr isKindOfClass:[NSArray class]]) {
                    
                    //如果是展开的，那数据源就保留全部数据,如果是收起的，那就展示5条数据
                    if (i == 5) {
                        if (formBgViewModel.isFormShowAllData == YES) {
                            [self.leftTableDataSource addObject:arr[0]];
                            continue;
                        } else {
                            break;
                        }
                    }
                    [self.leftTableDataSource addObject:arr[0]];
                    
                }
            }
            
            [self resetHeight:self.leftTableDataSource.count *kMachineFormTableViewRowHeight + kMachineFormTitleLabelHeight *2 + kMachineFormBottomBtnHeight + 40];
            [self.leftTableView resetHeight:self.leftTableDataSource.count *kMachineFormTableViewRowHeight];
            [self.formScrollView resetHeight:self.leftTableView.height + kMachineFormTitleLabelHeight];
            [self.showAllButton resetY:self.height - kMachineFormBottomBtnHeight - 20];
        }
        [self.leftTableView reloadData];
    } else {
        NSLog(@"没数据，清空");
        self.noDataLabel.hidden = NO;
        self.showAllButton.hidden = YES;
        NSArray *arrrr = @[@"", @"", @""];
        [self.leftTableDataSource addObject:arrrr];
        [self.leftTableView reloadData];
    }
    
    self.formScrollView.contentSize = CGSizeMake(kMachineFormContentTitleLabelWidth *(titArr.count - 1), self.formScrollView.height);
    self.formScrollView.formScrollViewModel = self.formBgViewModel;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(17, 17)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = maskPath.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask  = maskLayer;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
